package com.weiiew.backend.controller;

import com.weiiew.backend.entity.KeyInfo;
import com.weiiew.backend.entity.TerminationPolicy;
import com.weiiew.backend.entity.config.KeyConfig;
import com.weiiew.backend.executor.KeyboardExecutor;
import com.weiiew.backend.operationUtils.KeyCode;
import com.weiiew.backend.utils.ApiResponse;
import com.weiiew.backend.utils.listener.FirstKeyListener;
import com.weiiew.backend.utils.listener.HotkeyListenerManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/keyboard")
public class KeyboardClickController {
    private final KeyboardExecutor keyboardExecutor;
    private final SimpMessagingTemplate messagingTemplate;
    private final HotkeyListenerManager manager;
    private final FirstKeyListener firstKeyListener;
    private final KeyConfig config = new KeyConfig();

    @PostMapping("/insert")
    public ApiResponse insertKey() {
        config.getList().add(new KeyInfo(KeyCode.SPACE, false, false, false, false, 0, 100));
        return ApiResponse.ok("添加成功").append("config", config);
    }

    @PostMapping("/remove")
    public ApiResponse removeKey(int index) {
        config.getList().remove(index);
        return ApiResponse.ok("删除成功").append("config", config);
    }
    @PostMapping("/clear")
    public ApiResponse clearKey(){
        config.getList().clear();
        return ApiResponse.ok("清楚成功").append("config", config);
    }

    @PostMapping("/toggle")
    public ApiResponse toggleExecutor() {
        keyboardExecutor.startStopToggle(this.config);
        return ApiResponse.ok("成功切换执行器");
    }

    @PostMapping("/stop/condition")
    public ApiResponse updateStop(@RequestBody TerminationPolicy policy) {
        config.setPolicy(policy);
        return ApiResponse.ok("更新成功").append("config", config);
    }

    @PostMapping("/listener")
    public ApiResponse listenerKey(int index) {
        // 1. 参数校验
        if (index < 0 || index >= config.getList().size()) {
            return ApiResponse.failure("无效的索引");
        }
        KeyInfo old = config.getList().get(index);
        firstKeyListener.startFirstKeyListener((info) -> {
            info.setInterval(old.getInterval());
            info.setDuration(old.getDuration());
            config.getList().set(index, info);
            messagingTemplate.convertAndSend("/topic/keyboard-config", config);
        });
        return ApiResponse.ok("监听已启动");
    }
    @PostMapping("/bind")
    public ApiResponse bindListener() {
        manager.registerHotkey("keyboard.toggle", () -> keyboardExecutor.startStopToggle(this.config));
        return ApiResponse.ok("绑定成功").append("config", config);
    }
    @PostMapping("/unbind")
    public ApiResponse unbindListener(){
        manager.unregisterPageHotkeys("keyboard");
        return ApiResponse.ok("解绑成功");
    }
}